/**
 *  *2010-4-28 下午03:53:41  add 
 */
package com.base.myproject.client.busi.service.analysis;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CalendarUtils;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Administrator
 *2010-4-28
 * javazgw@gmail.com
 */
public class BranchAnalysis extends ContentPanel{
	SelectionListener<ButtonEvent> listener ;
	ChartModel model =null;
	BaseStore<JDBCModelData> store;
	DateField qdatefrom = new DateField();
	DateField qdateto = new DateField();
	 Chart chart =null;
	 DateTimeFormat activeFormat = DateTimeFormat.getFormat("yyyy-M-d");
	public BranchAnalysis()
	{
		init();
	}
	void init() {
		setWidth("800px");
		initlistener();
		initstore();
		initWidget();
		initQueryParamPanel();
		
		this.setAutoHeight(true);
		//this.setAutoWidth(true);
	}
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("finance1");
		type.addField("finance2");
		type.addField("systemname");
		type.addField("type");

		StoreInfo info = new StoreInfo();


		info.setTablename("sleeping");
		info.setNeedAllRow("no");
		// info.setSql("");
		store = new BaseStore<JDBCModelData>(type, info,false);

	}

	void initWidget() {
		 model = new ChartModel("店面营业对比",
				"font-size: 16px; font-family: Verdana; text-align: center;");
		model.setBackgroundColour("#fefefe");
		model.setLegend(new Legend(Position.TOP, true));
		model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);

		
		 BarChart bar = new BarChart(BarStyle.GLASS);
		  bar.setColour("#00aa00"); 
		  bar.setText("消费");
		  BarDataProvider barProvider = new	  BarDataProvider( "finance1","systemname","systemname");
		  barProvider.bind(store);
		 bar.setDataProvider(barProvider); 
		// bar.setTooltip("#val#");
		 //bar.addChartListener(listener);
		 model.addChartConfig(bar);
		 
		 bar = new BarChart(BarStyle.GLASS);  
		    bar.setColour("#ff6600");  
		    bar.setText("充值");
		    barProvider = new BarDataProvider("finance2","systemname","systemname");  
		    barProvider.bind(store);  
		    bar.setDataProvider(barProvider);  
		  //  bar.addChartListener(listener);  
		    model.addChartConfig(bar);  
		    
		    
//		 LineChart line = new LineChart();
//			line.setAnimateOnShow(true);
//			line.setText("消费额");
//			line.setColour("#ff6600");
//			LineDataProvider lineProvider = new LineDataProvider("finance","systemname");
//			lineProvider.bind(store);
//			line.setDataProvider(lineProvider);
//			model.addChartConfig(line);
		 
		 
		 String url = "chart/open-flash-chart.swf";
		  chart = new Chart(url);
			// chart.setToolTip("点击产品的图型看看");
			chart.setChartModel(model);
			chart.setHeight("400px");
//			chart.setAutoWidth(true);
			chart.setWidth("780px");
			
	}
	void initQueryParamPanel() {
		
		

		
		HorizontalPanel h = new HorizontalPanel();
		
		qdatefrom.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-M-d"));
		qdatefrom.setFieldLabel("起始时间：");
		qdateto.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-M-d"));
		qdateto.setFieldLabel("终止时间：");
		Button thisweek = new Button("查询");
		thisweek.addSelectionListener(listener);
		qdateto.setAllowBlank(false);
		qdatefrom.setAllowBlank(false);
		
		
		Date d = new Date();
		d = CalendarUtils.addDays(d,1);
		qdateto.setValue(d);
		qdatefrom.setValue(new Date());
		
		h.add(new Label("起始时间："));
		
		h.add(qdatefrom);
		h.add(new Label("终止时间："));
		h.add(qdateto);
		h.add(thisweek);
		
		VerticalPanel vpanel = new VerticalPanel();

		vpanel.add(new Html("<br>"));
		vpanel.add(h);
		vpanel.add(new Html("<br>"));
		// vpanel.add(CreateGrid());
		vpanel.add(chart);

		add(vpanel);
		
		
	}
	void initlistener()
	 {
		 listener = new SelectionListener<ButtonEvent>()
		 {
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					
				
					{
						
						if(qdatefrom.isValid() ||qdateto.isValid() )
						{
		
						StoreInfo info = store.getStoreinfo();
						String str = "";
						str+=" select sum(finance1) as finance1,sum(finance2) as finance2,systemname from( ";
						str+=" select case when type='消费' then sum(-1*finance)   end as finance1,case when type='充值' then sum(finance) end as finance2,systemname,type from (                                                                               ";
						str+="    select paid*-1 as finance ,ccode,bcode,serviceenddate as updatedate,'消费' as type,                                              ";
						str+=" null as finance_real,null as finance_gif from s_service_po  where busistatus=524288 and serviceenddate between '"+activeFormat.format(qdatefrom.getValue())+"'          ";
						str+=" and '"+activeFormat.format(qdateto.getValue())+"'                                                                                                                     ";
						str+=" union all                                                                                                                           ";
						str+=" select isnull(finance*dc,0) as finance,ccode,bcode,updatedate,'充值'  as type,                                                      ";
						str+=" finance_real,finance_gif  from  s_service_finance where updatedate between '"+activeFormat.format(qdatefrom.getValue())+"' and '"+activeFormat.format(qdateto.getValue())+"'                              ";
						str+="  ) as a                                                                                                                             ";
						str+=" left join     (     select cname,ccode,s_cardno from ccode ) as b   on  a.ccode = b.ccode                                           ";
						str+=" left join (  select bname,(select name from systeminfo where bcode.systemid = systeminfo.id) as systemname,bcode from bcode  ) as c ";
						str+="  on a.bcode = c.bcode                                                                                                               ";
						str+=" group by systemname,type                                                                                          ";
						str+=" ) as z  group by systemname order by systemname   ";
						info.setSql(str);
						store.reload();
						}
						else
						{
							MessageBox.alert("提示", "请录入时间段", null);
						}
						
						
					}
				}
		 };
		 
	 }
}
